<?php

class DB_WEIBO extends ORM_Table {
    
    static protected $table_name = 'weibo';
    
    static public function getTableName($params = null) {
        return self::$table_name;
    }
    static $users  = array();


    /**
     * 批量保存
     * @param type $users Ta的粉丝
     * @return type 
     */
    static function batch_save($users){
        
        if(empty($users)){
            return self::$users;
        }
        
        foreach ($users as $k => $v){
            
            $day_publish    =  time() - strtotime($v['created_at']);
            $day_publish    = round($day_publish /(3600*24),0);
            $day_publish    = round($v['statuses_count'] / $day_publish, 4);
            //添加标签
            //DB_TAG::store($v['id'], '获取中');
            
            //微博历史粉丝、博文
            //DB_HISTORY::store($v);
            
            $weibo   = ORM_Finder::findOne('DB_WEIBO', array('weibo_id'=>$v['id']),'id');
            
            if(!$weibo){
                $weibo  = new DB_WEIBO();
            }
            $weibo->setWeiboId($v['id']);
            $weibo->setScreenName($v['screen_name']);
            $weibo->setGender($v['gender']);
            $weibo->setProfileImageUrl($v['profile_image_url']);
            if($v['verified']){
                $weibo->setVerified(1);
            }
            $weibo->setFollowersCount($v['followers_count']);
            $weibo->setFriendsCount($v['friends_count']);
            $weibo->setStatusesCount($v['statuses_count']);
            $weibo->setFavouritesCount($v['favourites_count']);
            if($v['allow_all_act_msg']){
                $weibo->setAllowSend(1);
            }
            $created_at = date('Y-m-d',  strtotime($v['created_at']));
            $weibo->setCreatedAt($created_at);
            $weibo->setDayPublish($day_publish);
            $weibo->save();
            self::$users[]  = $weibo;
        }
        return self::$users;
    }
    
    /**
     * 获取我的粉丝信息
     * @param type $weibo_id_arr
     * @return type 
     */
    static function getMyFollower($weibo_id_arr){
        
        if(is_array($weibo_id_arr)){
            $weibo_id_arr   = implode(',', $weibo_id_arr);
        }
        
        $rs = ORM_Finder::find('DB_WEIBO', "weibo_id in ($weibo_id_arr)");
        return $rs;
    }
    

    /**
     *
     * @param type $weibo_id_arr
     * @param type $follower_count
     * @return type 
     */
    static function getMyFollowerCount($weibo_id_arr, $follower_count=null){
        
        if(is_array($weibo_id_arr)){
            $weibo_id_arr   = implode(',', $weibo_id_arr);
        }
        
        if(null !=$follower_count){
            $where  =  "weibo_id in ($weibo_id_arr) and followers_count>=".$follower_count;
        }else{
            $where  =  "weibo_id in ($weibo_id_arr)";
        }
        
        $rs = ORM_Finder::findCount('DB_WEIBO', $where);
        return $rs;
    }
}

?>
